package cn.android666.timbertree

import android.content.Context
import androidx.startup.Initializer
import cn.android666.timbertree.simple.BuildConfig
import timber.log.Timber

class MyTimberInitializer : Initializer<Unit> {

    override fun create(context: Context) {
        if (BuildConfig.DEBUG) {
            Timber.plant(DebugTreeWithThreadName(), FileTree(context))
        } else {
            Timber.plant(FileTree(context))
        }

        val defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
        Thread.setDefaultUncaughtExceptionHandler { t, e ->
            // 参数t为线程，表示当前发生异常的线程，这里的调用也是由t线程调用的
            Timber.e(e, "未捕获的异常")
            defaultUncaughtExceptionHandler?.uncaughtException(t, e)
        }

        Timber.d("Timber initialized by Startup from app")
    }

    override fun dependencies(): List<Class<out Initializer<*>>> {
        // 如果没有依赖其他 Startup 初始化器，就返回空列表
        return emptyList()
    }

}
